$file1 = $ARGV[0];
$range = $ARGV[1];
$whole = $ARGV[2];
$chop =  $ARGV[3];
$marker = $ARGV[4];
$new_whole = $ARGV[5];
$new_chop = $ARGV[6];
chomp($file1);
chomp($range);
chomp($whole);
chomp($chop);
chomp($marker);
open BUFF,"<$file1" or die "no file exists $file1\n";
open WHOLE,"<$whole", or die "no file exists $whole\n";
open CHOP," <$chop", or die "no file exists $chop\n";
open MARKER, " <$marker",or die "no file exists $marker\n";
open(WRBUFF1,">$new_whole");
open(WRBUFF2,">$new_chop");
$k =0;
$line = <WHOLE>;
$line = <CHOP>;
while($line = <BUFF> )
{
	chomp($line);
	if($line =~ m/Segment/)	
	{
		$k = 1;
	}
	if($k ==1 && $line =~ m/^\d/)
	{
		@array = split(" ",$line);
		$line = <BUFF>;
		chomp($line);
		@uparray = split(" ",$line);
		shift(@array);
		pop(@uparray);
		@combine = (@array,@uparray);
		@combine = sort {$a <=> $b} @combine;
		for($i=0;$i<@combine;$i++)
		{
			if($i%2 != 1)
			{
				$ulimit[$i] = $combine[$i];
				$llimit[$i] =  $combine[$i]-$range;
				
			}	
			else
			{
				$ulimit[$i] = $combine[$i]+$range;
				$llimit[$i] = $combine[$i];
			}
		}
	}
}
print "@combine\n@ulimit\n@llimit\n";
$ku =0;
while($line = <WHOLE>)
{
	chomp($line);
	$line1 = <CHOP>;
	chomp($line1);
	$line2 = <MARKER>;
	chomp($line2);
	@array = split(" ",$line);
	@array1 = split(" ",$line1);
	@array2 = split(" ",$line2);
	$ku++;
	print $ku."\n";	
	#print "$array[0]\t$array1[0]\t$array2[0]\n";
	if($array[0] eq $array1[0] && $array[0] eq $array2[0])
	{	
		for($i=0;$i<@ulimit;$i++)
		{

			if($array2[1] >= $llimit[$i] && $array2[1] <= $ulimit[$i])
			{
				#print "$array2[1]\t$llimit[$i]\t$ulimit[$i]\n";
				print WRBUFF1 $line."\n";
				print WRBUFF2 $line1."\n";
				last;
			}
			
		}
			
	}
	else
	{
		print "error\t$array[0]\t$array1[0]\t$array2[0]\n";
	}

}
#print "@combine\n@ulimit\n@llimit\n";

